package club.kingyin.kyblog.controller;

import club.kingyin.kyblog.core.business.dao.Doc;
import club.kingyin.kyblog.core.business.dao.Query;
import club.kingyin.kyblog.core.business.dto.ResultUtil;
import club.kingyin.kyblog.core.business.service.DocService;
import club.kingyin.kyblog.core.business.service.admin.TypeService;
import club.kingyin.kyblog.core.business.service.admin.SiteProService;
import club.kingyin.kyblog.core.business.vo.PageVo;
import club.kingyin.kyblog.core.business.vo.admin.ConfigVo;
import club.kingyin.kyblog.core.business.vo.admin.DocInfoVo;
import club.kingyin.kyblog.core.extend.IPSetPostProcessor;
import club.kingyin.kyblog.core.utils.cache.ClearCache;
import club.kingyin.kyblog.core.utils.log.LogConsole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * @author : kingyins
 * @version : 1.0
 * @program : ky-blog
 * @description : 在此处添加类描述
 * @create : 2021年08月25日 10:53
 **/
@RestController
@RequestMapping("/doc")
public class DocController {

    @Autowired
    private SiteProService siteProService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private DocService docService;

    @LogConsole(module = "获得文章图片集", operator = "九命可", sources = IPSetPostProcessor.class)
    @GetMapping("/urls")
    public ResultUtil getDocInfo() {
        DocInfoVo urls = siteProService.getDocInfo();
        if (urls == null) {
            return ResultUtil.fail("无法获得推荐图片");
        }
        return ResultUtil.success(urls);
    }

    // 缓存更新 /category
    @ClearCache(prefix = "WebSiteController::category", params = "*", module = "标签更新", operator = "九命可", sources = IPSetPostProcessor.class)
    @PostMapping("/type/update")
    public ResultUtil updateType(@RequestBody ConfigVo config) {
        if (typeService.updateType(config.getKey(), config.getValue())) {
            return ResultUtil.success("更新成功");
        } else {
            return ResultUtil.fail("更新失败");
        }
    }

    // 缓存更新 /category
    @ClearCache(prefix = "WebSiteController::category", params = "*", module = "标签删除", operator = "九命可", sources = IPSetPostProcessor.class)
    @PostMapping("/type/delete")
    public ResultUtil deleteType(@RequestBody ConfigVo config) {
        if (typeService.deleteType(config.getKey())) {
            return ResultUtil.success("删除成功");
        } else {
            return ResultUtil.fail("删除失败");
        }
    }

    // 缓存更新 /category
    @ClearCache(prefix = "WebSiteController::category", params = "*", module = "添加标签", operator = "九命可", sources = IPSetPostProcessor.class)
    @PostMapping("/type/add")
    public ResultUtil addTypes(@RequestBody ArrayList<String> types) {
        return typeService.addTypes(types)?ResultUtil.success("添加成功"):ResultUtil.fail("添加失败");
    }

    // 缓存更新 /article&id ｜ /post/list
    @ClearCache(prefix = {"WebDocController::getArticle", "WebDocController::searchList"}, params = {"id","*"}, module = "保存文档", operator = "九命可", sources = IPSetPostProcessor.class)
    @PostMapping("/save")
    public ResultUtil saveDoc(@RequestBody Doc doc) {
        // 待处理
        if (doc.getId() == null) {
            // 新增
            if (docService.createDoc(doc)) {
                return ResultUtil.success(doc.getId(),"已创建文章");
            } else ResultUtil.fail("保存失败");
        } else {
            // 修改
            if (docService.updateDoc(doc)) {
                return ResultUtil.success(doc.getId(),"published".equals(doc.getStatus())?"已发布文章":"已保存文章");
            } else return ResultUtil.fail("保存失败");
        }
        return ResultUtil.fail("保存失败");
    }

    @LogConsole(module = "获取文章列表", operator = "九命可", sources = IPSetPostProcessor.class)
    @PostMapping("/list")
    public ResultUtil getList(@RequestBody Query query) {
        setQuery(query);
        PageVo pageVo = docService.adminSearchList(query);
        if (pageVo == null) {
            return ResultUtil.fail("获取列表失败");
        }
        return ResultUtil.success(pageVo);
    }

    public void setQuery(Query query) {
        if ("".equals(query.getContext()))
            query.setContext(null);
        if ("".equals(query.getStatus()))
            query.setStatus(null);
    }


    @LogConsole(module = "后端获取文章", operator = "九命可", sources = IPSetPostProcessor.class)
    @GetMapping("/detail")
    public ResultUtil getDoc(@RequestParam("id") int id) {
        Doc doc = docService.getDocById(id);
        if (doc == null) {
            return ResultUtil.fail("没有这篇文章");
        }
        return ResultUtil.success(doc);
    }

    // 缓存更新 /article&id ｜ /post/list
    @ClearCache(prefix = {"WebDocController::getArticle", "WebDocController::searchList"}, params = {"id","*"}, module = "更新文章", operator = "九命可", sources = IPSetPostProcessor.class)
    @PostMapping("/update")
    public ResultUtil updateDoc(@RequestBody Doc doc) {
        if (docService.updateDoc(doc)) {
            return ResultUtil.success("更新成功");
        }
        return ResultUtil.fail("更新失败");
    }

    // 缓存更新 /article&id ｜ /post/list
    @ClearCache(prefix = {"WebDocController::getArticle", "WebDocController::searchList"}, params = {"true","*"}, module = "删除文章", operator = "九命可", sources = IPSetPostProcessor.class)
    @GetMapping("/delete")
    public ResultUtil deleteDoc(@RequestParam("id") int id) {
        if (docService.deleteById(id)) {
            return ResultUtil.success("删除成功");
        }
        return ResultUtil.fail("删除失败");
    }

    @LogConsole(module = "获得文章待审核评论", operator = "九命可")
    @GetMapping("/comment")
    public ResultUtil getComments(@RequestParam("id") int id) {
        return ResultUtil.success(docService.getCommentContextByDocId(id));
    }

    // 缓存更新 /comment&id
    @ClearCache(prefix = "WebDocController::getComments", params = "*", module = "评论通过审核", operator = "九命可", sources = IPSetPostProcessor.class)
    @GetMapping("/comment/publish")
    public ResultUtil publishComment(@RequestParam("id") int id) {
        if (docService.publishedComment(id)) {
            return ResultUtil.success("已通过审核");
        }
        return ResultUtil.fail("审核出现异常");
    }

    // 缓存更新 /comment&id
    @ClearCache(prefix = "WebDocController::getComments", params = "*", module = "删除评论", operator = "九命可", sources = IPSetPostProcessor.class)
    @GetMapping("/comment/delete")
    public ResultUtil deleteComment(@RequestParam("id") int id) {
        if (docService.delComment(id)) {
            return ResultUtil.success("已删除评论");
        }
        return ResultUtil.fail("删除失败");
    }

}
